######################################################
# Code to create Figure 2: Over Time Trends in Extremity Perception
# By Ideological Extremity
######################################################


############################
#Packages
############################
library(survey)
library(srvyr)
library(directlabels)
library(rio)
library(tidyverse)
library(ggstance)

############################
#Import Data and Clean
############################

#######Data
combdata <- import("./Data/pew_cnn_comb.Rda")

#######Declare Survey Design
comb_design <- combdata %>%
  filter(!is.na(weight)) %>%  #This removes some observations with missing weight data
  srvyr::as_survey_design(ids = 1, weights=weight)

########Get weighted means
ideol_means <- comb_design %>%
  mutate(too_extreme1 = factor(too_extreme1, 
                               levels=c(1,2,3), 
                               labels=c("Neither", "One Party", "Both Parties")), 
         ideol_ext = factor(ideol_ext, 
                            levels=c(1,2,3), 
                            labels=c("Moderate", "Liberal/Conservative", 
                                     "Very Liberal/Conservative"))) %>%
  filter(!is.na(too_extreme1), !is.na(ideol_ext)) %>%
  group_by(sdate, ideol_ext, too_extreme1) %>%
  summarize(proportion = srvyr::survey_mean(na.rm=T)) %>%
  mutate(lower = proportion - (1.96*proportion_se), 
         upper = proportion + (1.96*proportion_se), 
         sdate = as.Date(sdate))

#######Export Overall Means
export(ideol_means, "./Data/ideol_means.csv")

#######Figure
f1 <- ideol_means %>%
  ggplot(aes(x=sdate, y=proportion, shape=too_extreme1, linetype=too_extreme1)) + 
  geom_point() + 
  geom_smooth(method=lm, se=F, color="black") + 
  facet_wrap(~ideol_ext, labeller = label_wrap_gen(width = 15)) + 
  scale_y_continuous(limits=c(0,1)) + 
  theme_light(16) + 
  labs(title = "Mean Perceptions by Ideology", 
       y = "Proportion Giving Response", 
       x = "Time",
       shape = "Response", 
       linetype = "Response") + 
  scale_x_date(date_breaks= "6 year", date_labels="%Y") + 
  theme(legend.position = c(0.1, 0.9), 
        strip.text.x = element_text(face="bold", color="black"))  + 
  scale_shape_manual(values=c(17, 1, 0)) + 
  scale_linetype_manual(values=c(1, 3, 2))
  
f1a <- lemon::reposition_legend(f1, 'top left', offset= 0.005, 
                                panel = "panel-1-1")
  
#######################
#Mlogit Results
#From Stata
#######################

###############
#No Controls
###############

#Load, Clean, Bind the Data
neither_noc <- import("./Data/probs_1_nocontrol.dta") %>%
  rename(ame = `_margin`, 
         se = `_se`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`, 
         ideol = `_by1`) %>%
  mutate(outc = "Neither Party") %>%
  select(ame, se, lower, upper, ideol, outc)

one_noc <- import("./Data/probs_2_nocontrol.dta") %>%
  rename(ame = `_margin`, 
         se = `_se`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`, 
         ideol = `_by1`) %>%
  mutate(outc = "One Party") %>%
  select(ame, se, lower, upper, ideol, outc)

both_noc <- import("./Data/probs_3_nocontrol.dta") %>%
  rename(ame = `_margin`, 
         se = `_se`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`, 
         ideol = `_by1`) %>%
  mutate(outc = "Both Parties") %>%
  select(ame, se, lower, upper, ideol, outc)

ame_comb1 <- bind_rows(neither_noc, one_noc, both_noc) %>%
  mutate(outc = factor(outc, levels=c("Neither Party", "One Party", "Both Parties")), 
         ideol = factor(ideol, levels=c(1,2,3), 
                        labels=c("Moderate", "Lib.\n/Cons.", "Very Lib.\n/Cons.")))

#Figure
f1b <- ame_comb1 %>%
  ggplot(aes(y=ame, x=ideol, shape=outc)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  facet_grid(. ~ outc ) + 
  theme_light(16) + 
  geom_hline(yintercept=0) + 
  labs(title = "Effect of Year by Ideology", 
       y = NULL, x = NULL) + 
  theme(legend.position = "none", 
        strip.text.x = element_text(face="bold", color="black"), 
        axis.text.x = element_text(size=10)) + 
  scale_shape_manual(values=c(17,1,0))

#Combine with f1a above and save
ggpubr::ggarrange(f1a, f1b , 
                  labels=c("A", "B"))

ggsave("figure2.png", 
       height=8, width=14, 
       dpi=1200)

